package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * model.User: lucky
 * Date: 2022-10-24
 * Time: 0:53
 */

//用于封装博客列表的基本操作
public class BlogDao {
    //1.插入一个博客进博客表里
    public void insert(Blog blogs){
        //JDBC基本代码

        Connection connection = null;
        PreparedStatement statement = null;
        try{
            //1.数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造SQL语句
            String sql = "insert into blogs values(null, ?, ?, ?, now())";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blogs.getTitle());
            statement.setString(2,blogs.getContent());
            statement.setInt(3,blogs.getBlogId());
            //3.执行SQL
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.关闭连接，释放资源
            DBUtil.close(connection, statement, null);
        }
    }
    //2.获取博客表中的所有博客信息（用于详情页，部分每篇博客信息
    public List<Blog> selectAll(){
        List<Blog> blogs = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try{
            connection = DBUtil.getConnection();
            String sql = "select * from blogs";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                if(content.length() > 50){
                    content = content.substring(0,50) + "...";
                }
                blog.setContent(content);
                blog.setBlogId(resultSet.getShort("blogId"));
                blog.setPostTime(resultSet.getTimestamp("pastTime"));
                blogs.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return blogs;
    }
    //3.根据博客表Id获取指定的博客内容（用于博客详情页）
    public Blog selectOne(int blogId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try{
            connection = DBUtil.getConnection();
            String sql = "select * from blogs where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setBlogId(resultSet.getShort("blogId"));
                blog.setPostTime(resultSet.getTimestamp("pastTime"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection, statement , resultSet);
        }
        return null;
    }
    //4.博客列表中，根据查博客id删除博客
    public void deletd(int blogId){
        Connection connection = null;
        PreparedStatement statement = null;
        try{
            connection = DBUtil.getConnection();
            String sql = "delete from blogs where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection, statement, null);
        }

    }
}
